package com.gzhryc.shared_device.cdb.core.protocol.v1.msg;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.logging.Logger;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ACResponse implements Serializable {

	private static final long serialVersionUID = 1L;
	
	public final static Logger log = Logger.getLogger(ACResponse.class);

	public final static String CMD = "AC";

	private String cmd = CMD;
	private String messageId;
	private String deviceSn;
	private String imsi;
	private String iccid;
	private Integer rssi;

	private String version;
	private Integer slotTotal;
	private List<SlotItem> items;

	public ACResponse() {
	}

	public ACResponse(BaseMessage baseMessage) {
		String[] data = baseMessage.getData();
		this.messageId = baseMessage.getMessageId();

		if (data != null) {
			if (data.length >= 1) {
				this.deviceSn = data[0];
			}
			if (data.length >= 2) {
				this.imsi = data[1];
			}
			if (data.length >= 3) {
				this.iccid = data[2];
			}
			if (data.length >= 4) {
				if (StringUtils.isNotBlank(data[3]) && StringTools.isNumber(data[3])) {
					this.rssi = Integer.valueOf(data[3]);
				}
			}
			if (data.length >= 5) {
				this.version = data[4];
			}
			if (data.length >= 6) {
				if (StringUtils.isNotBlank(data[5]) && StringTools.isNumber(data[5])) {
					this.slotTotal = Integer.valueOf(data[5]);
				}
			}
			Integer total = data.length - 6;
			if (total > 0) {
				this.items = new ArrayList<>();
				Integer index = 6;
				for (int i = 0; i < total; i++) {
					String str = data[(index + i)];
					String[] array = str.split(":");
					SlotItem item = new SlotItem(array);
					this.items.add(item);
				}
			}
		}
	}

	public String getCmd() {
		return cmd;
	}

	public void setCmd(String cmd) {
		this.cmd = cmd;
	}

	public String getMessageId() {
		return messageId;
	}

	public void setMessageId(String messageId) {
		this.messageId = messageId;
	}

	public String getDeviceSn() {
		return deviceSn;
	}

	public void setDeviceSn(String deviceSn) {
		this.deviceSn = deviceSn;
	}

	public String getImsi() {
		return imsi;
	}

	public void setImsi(String imsi) {
		this.imsi = imsi;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public Integer getRssi() {
		return rssi;
	}

	public void setRssi(Integer rssi) {
		this.rssi = rssi;
	}

	public String getIccid() {
		return iccid;
	}

	public void setIccid(String iccid) {
		this.iccid = iccid;
	}

	public Integer getSlotTotal() {
		return slotTotal;
	}

	public void setSlotTotal(Integer slotTotal) {
		this.slotTotal = slotTotal;
	}

	public List<SlotItem> getItems() {
		return items;
	}

	public void setItems(List<SlotItem> items) {
		this.items = items;
	}

	public static class SlotItem {

		private Integer slotId;
		private String mobilePowerId;
		private Double supplyVolume; 	// 移动电源电量
		private String lockState; 		// 移动电源锁状态
		private Integer revertState; 	// 移动电源归还状态

		public SlotItem() {
		}

		public SlotItem(String[] data) {
			if (data != null) {
				if (data.length >= 1) {
					String temp = data[0];
					if (StringUtils.isNotBlank(temp) && StringTools.isNumber(temp)) {
						this.slotId = Integer.valueOf(temp);
					}
				}
				if (data.length >= 2) {
					String temp = data[1];
					if (StringUtils.isNotBlank(temp) && StringTools.isNumber(temp)) {
						this.revertState = Integer.valueOf(temp);
					}
				}
				if (data.length >= 3) {
					this.mobilePowerId = data[2];
				}
				if (data.length >= 4) {
					String temp = data[3];
					if (StringUtils.isNotBlank(temp) && StringTools.isNumber(temp)) {
						this.supplyVolume = Double.valueOf(temp);
					}
				}
				if (data.length >= 5) {
					this.lockState = data[4];
				}
			}
		}

		public Integer getSlotId() {
			return slotId;
		}

		public void setSlotId(Integer slotId) {
			this.slotId = slotId;
		}

		public String getMobilePowerId() {
			return mobilePowerId;
		}

		public void setMobilePowerId(String mobilePowerId) {
			this.mobilePowerId = mobilePowerId;
		}

		public Double getSupplyVolume() {
			return supplyVolume;
		}

		public void setSupplyVolume(Double supplyVolume) {
			this.supplyVolume = supplyVolume;
		}

		public String getLockState() {
			return lockState;
		}

		public void setLockState(String lockState) {
			this.lockState = lockState;
		}

		public Integer getRevertState() {
			return revertState;
		}

		public void setRevertState(Integer revertState) {
			this.revertState = revertState;
		}
	}
}
